package com.cpm.offcn.helloword;

import com.cpm.offcn.utils.RabbitUtils;
import com.rabbitmq.client.*;

import javax.sound.midi.Receiver;
import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Consumer {
    public static void main(String[] args) throws IOException, TimeoutException {
        //获取tcp长连接
        Connection connection = RabbitUtils.getConnection();
         //创建通信通道
        Channel channel = connection.createChannel();

        //启动一个消费者
        channel.basicConsume("QUEUE",false,new Reciver(channel));
    }
}


class Reciver extends DefaultConsumer{
    private Channel channel;

    public Reciver(Channel channel) {
        super(channel);
        this.channel=channel;
    }

    @Override
    public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {

        String message = new String(body);
        System.out.println("消费者接收到的消息："+message);

        System.out.println("消息的TagId："+envelope.getDeliveryTag());
        //false只确认签收当前的消息，设置为true的时候则代表签收该消费者所有未签收的消息
        channel.basicAck(envelope.getDeliveryTag(), false);
    }

}
